class Ucon64 < Formula
  desc "ROM backup tool and emulator's Swiss Army knife program"
  homepage "https://ucon64.sourceforge.io/"
  url "https://downloads.sourceforge.net/ucon64/ucon64-2.1.0-src.tar.gz"
  sha256 "c99964060a5337cea811b27c4103e186a14ba1f04b19cff08bac0260271bc872"
  head "https://svn.code.sf.net/p/ucon64/svn/trunk/ucon64"

  bottle do
    sha256 "8b564dffd204edc6ae3f879f52988fe89baf4db14534d8fa59516a07fec9fc20" => :catalina
    sha256 "84609c8e92dae09a76f12eebe5c19d1769eb22d28029db5d15c14949800c358f" => :mojave
    sha256 "3fc97894b8edaa28fa90a2cb494976be49fa7a28fb875797bd5f56ddc3b2fd69" => :high_sierra
    sha256 "6fe6e1cf1db14e4592eb3486c6629bff06813a42c34b9ae8e188a5ad02745344" => :sierra
    sha256 "015cfd4803ea86f02429fc9a1f5de59d72012c9d0e32a92c9f996922ef422084" => :el_capitan
  end

  resource "super_bat_puncher_demo" do
    url "http://morphcat.de/superbatpuncher/Super%20Bat%20Puncher%20Demo.zip"
    sha256 "d74cb3ba11a4ef5d0f8d224325958ca1203b0d8bb4a7a79867e412d987f0b846"
  end

  def install
    # ucon64's normal install process installs the discmage library in
    # the user's home folder. We want to store it inside the prefix, so
    # we have to change the default value of ~/.ucon64rc to point to it.
    # .ucon64rc is generated by the binary, so we adjust the default that
    # is set when no .ucon64rc exists.
    inreplace "src/ucon64_misc.c", 'PROPERTY_MODE_DIR ("ucon64") "discmage.dylib"',
                                   "\"#{opt_prefix}/libexec/libdiscmage.dylib\""

    cd "src" do
      system "./configure", "--disable-debug",
                            "--disable-dependency-tracking",
                            "--disable-silent-rules",
                            "--prefix=#{prefix}"
      system "make"
      bin.install "ucon64"
      libexec.install "libdiscmage/discmage.so" => "libdiscmage.dylib"
    end
  end

  def caveats
    <<~EOS
      You can copy/move your DAT file collection to $HOME/.ucon64/dat
      Be sure to check $HOME/.ucon64rc for configuration after running uCON64
      for the first time.
    EOS
  end

  test do
    resource("super_bat_puncher_demo").stage testpath

    assert_match "00000000  4e 45 53 1a  08 00 11 00  00 00 00 00  00 00 00 00",
                 shell_output("#{bin}/ucon64 \"#{testpath}/Super Bat Puncher Demo.nes\"")
  end
end
